/*
 * Device.cpp
 *
 *  Created on: Apr 16, 2012
 *      Author: jaap
 */
#include <cassert>
#include <iostream>
#include <fcntl.h>
#include <stdlib.h>

using namespace std;

#include "Device.h"

Device::Device(const char* incomingDevice) {
	assert(incomingDevice != 0);
	currentDevice = ::open(incomingDevice, O_RDONLY);

	if (currentDevice < 0) {
		cerr << "ERROR opening " << incomingDevice << endl;
		exit(1);
	}
}

Block* Device::getBlock(int number) {

	if (cache.find(number)==cache.end()) {
		Block *bp = new Block( number );

		lseek(currentDevice, DBLKSIZ * number, SEEK_SET);
		read(currentDevice, bp->bytes, DBLKSIZ);

		cache[number] = bp;
	}
	return cache[number];
}
